package 左哥算法.ch07图;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

//深度优先题目
public class 城市的最大开销 {
    public static void main(String[] args) {
        Reader sc=new Reader();
        int n = sc.nextInt();
        Graph graph = new Graph(n);
        for (int i = 1; i <= n; i++) {
            graph.insert(i,sc.nextLine().split(" "));
        }
        System.out.println(get(graph));
    }
    public static int get(Graph graph){
        int n = graph.nodes.length;
        if (n==1)return graph.nodes[1].val;
        int max=0;
        for (Node v : graph.nodes) {
            if (v==null)continue;
            max=Math.max(max,fun(graph,v,new boolean[n]));
        }
        return max;
    }
    /**
     * 从node点出发，能拿到的最大值
     * @return
     */
    public static int fun(Graph graph, Node node, boolean[] set){
        if (set[node.id])return 0;//已经走过
        set[node.id]=true;
        int res=0;
        for (Node next : node.nexts) {
            graph.remove(next);   //将能抵达点全移除，因为他们不可能比当前点开销大
            res=Math.max(res,fun(graph,next,set));  //走所有邻居
        }
        set[node.id]=false;
        return res+ node.val;
    }
    static class Node{
        int id;
        int in; //入度
        int val;
        Node[] nexts;

        public Node(int id) {
            this.id = id;
        }
        public void init(int val,int size){
            this.val=val;
            nexts=new Node[size];
        }
    }
    static class Graph{
        Node[] nodes;
        public Graph(int size){
            nodes=new Node[size+1];
            for (int i = 1; i < nodes.length; i++) {
                nodes[i]=new Node(i);
            }
        }
        public void remove(Node o){
            nodes[o.id]=null;
        }

        public void insert(int id,String[] edge) {
            nodes[id].init(Integer.parseInt(edge[0]),edge.length-1);
            nodes[id].nexts=new Node[edge.length-1];
            for (int i = 1; i < edge.length; i++) {
                int next = Integer.parseInt(edge[i]);
                nodes[id].nexts[i-1]=nodes[next];
            }
        }
    }
    static class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
    }

}
